local cjson = require "cjson"
local mysql = require "resty.mysql"

local resp = {
    message = nil,
    success = false
}

local db = mysql:new()
local ok, err, errcode, sqlstate = db:connect({
    host = "127.0.0.1",
    port = 3306,
    database = "dataway",
    user = "root",
    password = "Lzslov123!"})

if not ok then
    resp.message = string.format([[bad result #1: %s]], err)
    ngx.say(cjson.encode(resp))
    ngx.exit(200)
end

local sql = [[select id, modifier, location from location_tab where 1=1]]
local res, err = db:query(sql)
if not res then
    resp.message = string.format([[bad result #1: %s]], err)
    ngx.say(cjson.encode(resp))
    ngx.exit(200)
end

local locations = require("resty.locations")
local my_locs = locations:new()
local data = {}
for _, v in pairs(res) do 
    table.insert(data, {location = v.location, id = v.id, modifier=v.modifier})
end

ngx.shared.DICT:delete("my_locs")
ngx.shared.DICT:set("my_locs", cjson.encode(data))
ngx.say(cjson.encode({success=true, keys = ngx.shared.DICT:get_keys()}))